package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/remove-nodes-from-linked-list/description/
 * @create 2025/9/10 18:35
 * @since JDK17
 */

public class Solution13 {
    static ListNode[] stack = new ListNode[100010];
    int top;
    public ListNode removeNodes(ListNode head) {
        while (head != null) {
            while (top > 0 && stack[top - 1].val < head.val) {
                top--;
            }
            stack[top++] = head;
            head = head.next;
        }
        ListNode list = stack[0], p = list;
        for (int i = 1; i < top; i++) {
            p = p.next = stack[i];
        }
        p.next = null;
        return list;
    }
}
